﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAI_0 : player
{

    SortedList<string, int> toScore;
    // Start is called before the first frame update
    void Start()
    {
        toScore = new SortedList<string, int>();
        toScore.Add("_*_", 10);//*号代表有子；_代表未被堵住
        toScore.Add("_**_", 100);

        toScore.Add("_***", 500);
        toScore.Add("***_", 500);
        toScore.Add("_***_", 1000);

        toScore.Add("****_", 5000);
        toScore.Add("_****", 5000);
        toScore.Add("_****_", 10000);

        toScore.Add("*****", 100000);
    }


    int CheckOneLine(int[] pos, int[] offset, int chess)
    {
        string temp = "*";

        for (int x = pos[0] + offset[0], y = pos[1] + offset[1]; x < 15 && x >= 0 && y < 15 && y >= 0; x += offset[0], y += offset[1])
        {
            if (board.grid[x, y] == (int)chess)//棋子颜色相等
            {
                temp = temp + "*";
            }
            else if (board.grid[x, y] == 0)//没有棋子
            {
                temp = temp + "_";
                break;
            }
            else
            {
                break;
            }
        }
        for (int x = pos[0] - offset[0], y = pos[1] - offset[1]; x < 15 && x >= 0 && y < 15 && y >= 0; x -= offset[0], y -= offset[1])
        {
            if (board.grid[x, y] == (int)chess)//棋子颜色相等
            {
                temp = "*" + temp;
            }
            else if (board.grid[x, y] == 0)//没有棋子
            {
                temp = "_" + temp;
                break;
            }
            else
            {
                break;
            }
        }
        int maxScore = 0;
        foreach (var item in toScore)
        {
            if (temp.Contains(item.Key))
            {
                if (toScore[item.Key] > maxScore)
                {
                    maxScore = toScore[item.Key];

                }
                Debug.Log(item.Key);

            }

        }




        return maxScore;
    }

    //返回某一点上的评价分数
    int SetScore(int[] pos)
    {
        int score = 0;
        score += CheckOneLine(pos, new int[] { 1, 1 }, 1);
        score += CheckOneLine(pos, new int[] { 0, 1 }, 1);
        score += CheckOneLine(pos, new int[] { 1, 0 }, 1);
        score += CheckOneLine(pos, new int[] { 1, -1 }, 1);
        score += CheckOneLine(pos, new int[] { 1, 1 }, 2);
        score += CheckOneLine(pos, new int[] { 0, 1 }, 2);
        score += CheckOneLine(pos, new int[] { 1, 0 }, 2);
        score += CheckOneLine(pos, new int[] { 1, -1 }, 2);
        return score;
    }


    // Update is called once per frame
    void Update()
    {

    }
    public override void Play()
    {
        //Debug.Log("AI0");
        //int x = Random.Range(0, 15), y = Random.Range(0, 15);
        //board.PlayChess(new int[] { x, y });//随机落子


        //Debug.Log(SetScore(new int[] { 7, 7 }));

        int maxX = 7, maxY = 7, maxScore = 40;
        if (board.grid[7, 7] != 0)
            maxScore = 0;
        for (int x = 0; x < 14; x++)
            for (int y = 0; y < 14; y++)
            {
                if (board.grid[x, y] != 0)//有子的地方就跳过
                    continue;
                int score = SetScore(new int[] { x, y });
                if (score > maxScore)
                {
                    maxX = x;
                    maxY = y;
                    maxScore = score;

                }

            }

        board.PlayChess(new int[] { maxX, maxY });



    }
}